//
//  SexAlertView.m
//  ZJControlsClass
//
//  Created by apple on 2024/11/5.
//

#import "SexAlertView.h"
#import "HomeMySlipleModel.h"
    
@interface SexAlertView ()<UIPickerViewDelegate,UIPickerViewDataSource>{
    NSInteger Index;
    UIView * topV;
}
@property (nonatomic,strong) NSMutableArray * showArr;
@property (nonatomic,strong) UIPickerView * pickerView;



@end




@implementation SexAlertView

-(void)setTypeBool:(BOOL)TypeBool{
    _TypeBool = TypeBool;
}
-(void)setDataSource:(NSMutableArray *)dataSource{
    _dataSource = dataSource;
    
    if (self.TypeBool){ // 部门
        
        self.showArr = [NSMutableArray arrayWithArray:[HomeMySlipleOrgListModel mj_objectArrayWithKeyValuesArray:self.dataSource]];
        
    }else{ // 男女 背景
        self.showArr = [NSMutableArray arrayWithArray:dataSource];
        
    }
    
}



- (instancetype)init
{
    self = [super initWithFrame:[UIApplication sharedApplication].keyWindow.frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.3];
        
        topV = [[UIView alloc] initWithFrame:CGRectMake(0,SCREEN_HEIGHT , SCREEN_WIDTH, 40)];
        topV.backgroundColor = COLOR_RGBA(242, 242, 242, 1.0);
        [self addSubview:topV];

        UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        cancelBtn.frame = CGRectMake(0, 0, 100, 40);
        [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        [cancelBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [cancelBtn.titleLabel setFont:[UIFont systemFontOfSize:16]];
        [cancelBtn addTarget:self action:@selector(respondsTo_checkCancel) forControlEvents:UIControlEventTouchUpInside];
        [topV addSubview:cancelBtn];

        UIButton *yesBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        yesBtn.frame = CGRectMake(SCREEN_WIDTH - 100, 0, 100, 40);
        [yesBtn setTitle:@"完成" forState:UIControlStateNormal];
        [yesBtn setTitleColor:COLOR_RGBA(69, 186, 155, 1) forState:UIControlStateNormal];
        [yesBtn.titleLabel setFont:[UIFont systemFontOfSize:16]];
        [yesBtn addTarget:self action:@selector(respondsTo_checkyes) forControlEvents:UIControlEventTouchUpInside];
        [topV addSubview:yesBtn];

        _pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(topV.frame), SCREEN_WIDTH, 207)];
        _pickerView.dataSource = self;
        _pickerView.delegate = self;
        _pickerView.backgroundColor = [UIColor whiteColor];
        [self addSubview:_pickerView];
        
        [_pickerView selectRow:0 inComponent:0 animated:YES];
        [self pickerView:_pickerView didSelectRow:0 inComponent:0];
        [UIView animateWithDuration:0.25 animations:^{
            topV.frame = CGRectMake(0, SCREEN_HEIGHT - 247, SCREEN_WIDTH, 40);
            _pickerView.frame = CGRectMake(0, CGRectGetMaxY(topV.frame), SCREEN_WIDTH, 207);
        }];
        
    }
    return self;
}

#pragma mark -UIPickerView
#pragma mark UIPickerView的数据源
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    
    return self.dataSource.count;
}


-(void)respondsTo_checkCancel{
    [self remove];
}
-(void)respondsTo_checkyes{
    [self remove];
    NSString *timeStr;
    timeStr = [NSString stringWithFormat:@"%@",((UILabel *)[_pickerView viewForRow:Index forComponent:0]).text];

    _block(timeStr);
    
}


- (void)remove {

    [UIView animateWithDuration:0.25 animations:^{

        self->topV.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 40);
        self->_pickerView.frame = CGRectMake(0, CGRectGetMaxY(self->topV.frame), SCREEN_WIDTH, 207);

    } completion:^(BOOL finished) {

        [self removeFromSuperview];
    }];

}

#pragma mark -- UIPickerViewDelegate,UIPickerViewDataSource
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    Index = row;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)),dispatch_get_main_queue() , ^{
        UILabel * label = (UILabel *)[pickerView viewForRow:row forComponent:component];
        label.textColor = COLOR_RGB(26, 174, 135);
        label.font = FONT16;
    });
}
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    // 设置文字的属性
    UILabel * sexLabel = [UILabel new];
    sexLabel.textAlignment = NSTextAlignmentCenter;
    sexLabel.textColor = COLOR_RGB(153, 153, 153);
    sexLabel.font = FONT14;
    
    if (self.TypeBool){
        HomeMySlipleOrgListModel * model = self.showArr[row];
        sexLabel.text = model.name;
    }else{
        sexLabel.text = self.showArr[row];
    }
    
    return sexLabel;
}










@end
